package com.openxt.nbzj.util;

import android.app.ActivityManager;
import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.telephony.TelephonyManager;
import android.util.Base64;

import com.alibaba.fastjson.JSON;
import com.openxt.nbzj.model.ConfigModel;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 * Created by 颖杰_2 on 2016/7/6.
 */
public class SystemUtil {
    /*
     * converts dip to px
     */
    public static int dip2Px(float dip, Context context) {
        return (int) (dip * context.getResources().getDisplayMetrics().density + 0.5f);
    }

    public static Bitmap getImageFromAssetsFile(String fileName, Context context) {
        Bitmap image = null;
        AssetManager am = context.getResources().getAssets();
        try {
            InputStream is = am.open(ConfigModel.ROOTPATH + fileName);
            image = BitmapFactory.decodeStream(is);
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return image;
    }

    /**
     * string转成bitmap
     *
     * @param data
     */
    public static Bitmap convertStringToBitmap(String data) {
        try {
            data = data.substring(data.indexOf(",") + 1);
            byte[] bitmapArray = Base64.decode(data, Base64.DEFAULT);
            return BitmapFactory.decodeByteArray(bitmapArray, 0, bitmapArray.length);
        } catch (Exception e) {
            return null;
        }
    }

    public static JSONArray getSubscribeList(Context context) {
        String fileName = "json/subscribe.json";
        AssetManager am = context.getResources().getAssets();
        try {
            StringBuilder sb = new StringBuilder();
            InputStream is = am.open(fileName);
            BufferedReader bufferedReader = null;
            String line = null;
            try {
                bufferedReader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                while ((line = bufferedReader.readLine()) != null) {
                    sb.append(line);
                }
                bufferedReader.close();
                is.close();

                return JSON.parseArray(sb.toString());
            } catch (Exception e) {
                throw e;
            } finally {
                if (bufferedReader != null)
                    bufferedReader.close();
                if (is != null)
                    is.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 判断应用是否在后台
     *
     * @param context
     * @return
     */
    public static boolean isBackground(Context context) {
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
        for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
            if (appProcess.processName.equals(context.getPackageName())) {
                if (appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND) {
                    return true;
                } else {
                    return false;
                }
            }
        }
        return false;
    }

    /**
     * 获取设备信息
     *
     * @param context
     * @return
     */
    public static JSONObject getDevice(Context context) {
        //http://blog.csdn.net/wenlele/article/details/6316750
        try {
            JSONObject map = new JSONObject();
            TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            map.put("IMEI", tm.getDeviceId());
            map.put("PhoneName", new Build().MODEL);
            map.put("PhoneType", "Android");
            map.put("SystemVersion", tm.getDeviceSoftwareVersion());

            return map;
        } catch (Exception e) {
            JSONObject map = new JSONObject();
            map.put("IMEI", "");
            map.put("PhoneName", "");
            map.put("PhoneType", "Android");
            map.put("SystemVersion", "");
            return map;
        }
    }

    public static Bitmap drawableToBitmap(Drawable drawable) {
        Bitmap bitmap = null;

        if (drawable instanceof BitmapDrawable) {
            BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
            if (bitmapDrawable.getBitmap() != null) {
                return bitmapDrawable.getBitmap();
            }
        }

        if (drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
            bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); // Single color bitmap will be created of 1x1 pixel
        } else {
            bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
        }

        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        drawable.draw(canvas);
        return bitmap;
    }
}
